package com.boc.framework.generator;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FreemarkerDemo {


    public static void main(String[] args) {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);

        Writer out = null;
        try {
            configuration.setDirectoryForTemplateLoading(new File("elephant-code-generator/src/main/resources/template"));
            Map<String, Object> map = new HashMap<>();
            map.put("classPath", "com.boc.framework.generator");
            map.put("className", "HelloGenerator");
            map.put("helloStr", "Hello World, It is first generated java file...");
            Template template = configuration.getTemplate("hello.ftl");
            File javaFile = new File("elephant-code-generator/src/main/java/com/boc/framework/generator/HelloGenerator.java");
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(javaFile)));
            template.process(map, out);
            System.out.println("文件导出成功~~");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
